package com.ruoyi.admin.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.admin.domain.ZhxBook;
import com.ruoyi.admin.domain.ZhxJielong;
import com.ruoyi.admin.domain.vo.ZhxSubscribeListAndTotal;
import com.ruoyi.admin.mapper.ZhxBookMapper;
import com.ruoyi.admin.mapper.ZhxJielongMapper;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.admin.mapper.ZhxSubscribeMapper;
import com.ruoyi.admin.domain.ZhxSubscribe;
import com.ruoyi.admin.service.IZhxSubscribeService;

/**
 * 用户订阅Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-11-11
 */
@Service
public class ZhxSubscribeServiceImpl implements IZhxSubscribeService 
{
    @Autowired
    private ZhxSubscribeMapper zhxSubscribeMapper;

    @Autowired
    private ZhxBookMapper zhxBookMapper;

    @Autowired
    private ZhxJielongMapper zhxJielongMapper;

    @Autowired
    private RedisCache redisCache;

    /**
     * 查询用户订阅
     * 
     * @param subscribeId 用户订阅主键
     * @return 用户订阅
     */
    @Override
    public ZhxSubscribe selectZhxSubscribeBySubscribeId(Long subscribeId)
    {
        return zhxSubscribeMapper.selectZhxSubscribeBySubscribeId(subscribeId);
    }

    /**
     * 查询用户订阅列表
     * 
     * @param zhxSubscribe 用户订阅
     * @return 用户订阅
     */
    @Override
    public List<ZhxSubscribe> selectZhxSubscribeList(ZhxSubscribe zhxSubscribe)
    {
        return zhxSubscribeMapper.selectZhxSubscribeList(zhxSubscribe);
    }

    /**
     * 查询用户订阅列表(public)
     *
     * @param zhxSubscribe 用户订阅
     * @return 用户订阅
     */
    @Override
    public ZhxSubscribeListAndTotal selectZhxSubscribeListTodo(ZhxSubscribe zhxSubscribe)
    {
        ZhxSubscribeListAndTotal zhxSubscribeListAndTotal = new ZhxSubscribeListAndTotal();
        zhxSubscribe.setUserId(SecurityUtils.getUserId());
        long total = 0;
        List<ZhxSubscribe> subscribes = zhxSubscribeMapper.selectZhxSubscribeList(zhxSubscribe);
        for (ZhxSubscribe subscribe : subscribes) {
            if (subscribe.getSubscribeType().equals("1")) {
                ZhxBook zhxBook = zhxBookMapper.selectZhxBookByBookId(subscribe.getBookId());
                subscribe.setSubscribeObject(zhxBook);
            }
            if (subscribe.getSubscribeType().equals("2")) {
                ZhxJielong zhxJielong = zhxJielongMapper.selectZhxJielongByJielongId(subscribe.getJielongId());
                subscribe.setSubscribeObject(zhxJielong);
            }
            total = total + subscribe.getNewQuantity();
        }
        zhxSubscribeListAndTotal.setZhxSubscribeList(subscribes);
        zhxSubscribeListAndTotal.setTotal(total);
        return zhxSubscribeListAndTotal;
    }

    /**
     * 新增用户订阅
     * 
     * @param zhxSubscribe 用户订阅
     * @return 结果
     */
    @Override
    public int insertZhxSubscribe(ZhxSubscribe zhxSubscribe)
    {
        zhxSubscribe.setCreateTime(DateUtils.getNowDate());
        return zhxSubscribeMapper.insertZhxSubscribe(zhxSubscribe);
    }

    /**
     * 新增用户订阅(public)
     *
     * @param zhxSubscribe 用户订阅
     * @return 结果
     */
    @Override
    public int insertZhxSubscribeTodo(ZhxSubscribe zhxSubscribe)
    {
        zhxSubscribe.setUserId(SecurityUtils.getUserId());
        zhxSubscribe.setCreateTime(DateUtils.getNowDate());

        // redis数据新增
//        Map<String, Long> zhxSubscribeCache = new HashMap<>();
//        zhxSubscribeCache = redisCache.getCacheObject("zhxSubscribeCache");
//        if (zhxSubscribe.getSubscribeType().equals("1")) {
//            zhxSubscribeCache.put(zhxSubscribe.getUserId() + "-1-" + zhxSubscribe.getBookId(), zhxSubscribe.getNewQuantity());
//        } else {
//            zhxSubscribeCache.put(zhxSubscribe.getUserId() + "-2-" + zhxSubscribe.getJielongId(), zhxSubscribe.getNewQuantity());
//        }
//        redisCache.setCacheObject("zhxSubscribeCache", zhxSubscribeCache);

        return zhxSubscribeMapper.insertZhxSubscribe(zhxSubscribe);
    }

    /**
     * 修改用户订阅
     * 
     * @param zhxSubscribe 用户订阅
     * @return 结果
     */
    @Override
    public int updateZhxSubscribe(ZhxSubscribe zhxSubscribe)
    {
        zhxSubscribe.setUpdateTime(DateUtils.getNowDate());
        return zhxSubscribeMapper.updateZhxSubscribe(zhxSubscribe);
    }

    /**
     * 批量删除用户订阅
     * 
     * @param subscribeIds 需要删除的用户订阅主键
     * @return 结果
     */
    @Override
    public int deleteZhxSubscribeBySubscribeIds(Long[] subscribeIds)
    {
        return zhxSubscribeMapper.deleteZhxSubscribeBySubscribeIds(subscribeIds);
    }

    /**
     * 删除用户订阅信息
     * 
     * @param subscribeId 用户订阅主键
     * @return 结果
     */
    @Override
    public int deleteZhxSubscribeBySubscribeId(Long subscribeId)
    {
        return zhxSubscribeMapper.deleteZhxSubscribeBySubscribeId(subscribeId);
    }

    /**
     * 删除用户订阅信息(public)
     *
     * @param subscribeId 用户订阅主键
     * @return 结果
     */
    @Override
    public int deleteZhxSubscribeBySubscribeIdTodo(Long subscribeId)
    {
        return zhxSubscribeMapper.deleteZhxSubscribeBySubscribeId(subscribeId);
    }

    /**
     * 清零用户订阅新增数量(public)
     *
     * @param subscribeId 用户订阅主键
     * @return 结果
     */
    @Override
    public int updateNewQuantityBySubscribeId(Long subscribeId)
    {
        return zhxSubscribeMapper.updateNewQuantityBySubscribeId(subscribeId);
    }


}
